package de.carcophan.jimdb.ejb.entites.service;

import de.carcophan.jimdb.ejb.entites.UserMovies;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class UserMoviesFacade extends AbstractFacade<UserMovies> {

    @PersistenceContext(unitName = "jimdb-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UserMoviesFacade() {
        super(UserMovies.class);
    }

    public Map<String, UserMovies> findByImdbIds(Set<String> searchMovies, Long userId) {
        Map<String, UserMovies> dbMovies = new HashMap<String, UserMovies>();
        for (String idForSite : searchMovies) {
            List<UserMovies> result = em.createQuery("select um from UserMovies um where um.movie.idForSite=:imdbId AND um.user.id=:userId").setParameter("imdbId", idForSite).setParameter("userId", userId).getResultList();
            for (UserMovies userMovies : result) {
                dbMovies.put(userMovies.getMovie().getIdForSite(), userMovies);
            }
        }
        return dbMovies;
    }

    public List<UserMovies> findByStatus(String status, Long userId) {
        return em.createQuery("select um from UserMovies um where um.user.id=:userId and um.movieStatus.movieStatus=:status").setParameter("userId", userId).setParameter("status", status).getResultList();

    }

    public List<UserMovies> findMoviesByUserId(Long id) {
        return (List<UserMovies>) em.createQuery("select um from UserMovies um where um.user.id=:id").setParameter("id", id).getResultList();
    }
}
